/*
* @Author: zhangfan
* @Date:   2017-06-03 21:50:16
* @Last Modified by:   anchen
* @Last Modified time: 2017-06-04 11:28:43
 */

package main

import (
	"fmt"
	"io/ioutil"
	"math/rand"
	"net/http"
	"sync"
	"time"
)

func crawle(url string, wg *sync.WaitGroup) (string, error) {

	fmt.Println(url)
	resp, err := http.Get(url)
	if err != nil {
		fmt.Println(err)
	}
	defer wg.Done()
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	fmt.Printf("req url:%s,resp:%s\n", url, string(body))
	return string(body), err
}

func random(min, max int) int {
	rand.Seed(time.Now().Unix())
	return rand.Intn(max-min) + min
}

func main() {
	start := time.Now()
	var wg sync.WaitGroup
	// urls := []string{"http://localhost:8080/api/valid/plate?plate=浙A12435", "http://localhost:8080/api/valid/plate?plate=浙A1A523"}

	// for _, url := range urls {

	// 	wg.Add(1)
	// 	go crawle(url, &wg)
	// }
	rand.Seed(time.Now().Unix())
	max := 99999
	min := 10000
	for i := 0; i < 5; i++ {
		url := fmt.Sprintf("http://localhost:8080/api/valid/plate?plate=浙A%d", rand.Intn(max-min)+min)
		wg.Add(1)
		go crawle(url, &wg)
	}

	wg.Wait()
	elapsed := time.Since(start)
	fmt.Printf("HttpClient took %s", elapsed)
	//time.Sleep(time.Second * 3)
	fmt.Println("Finished")
}
